package ONLINE_EXAM;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class hw_4 {
    /**
     * 借助队列 直到队列长度为1
     * 用一个数进行统计 pop一个push一个 整除3时 光pop
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String n ;
        while ((n= br.readLine())!= null){
            System.out.println(helper(Integer.valueOf(n)));

        }



    }

    public static int helper(int n){
        Queue<Integer> queue = new LinkedList<>();
        for (int i= 0;i<n;i++){
            queue.add(i);
        }
        int count = 0;
        while (queue.size() != 1){
            int out = queue.poll();
            count++;
            if (count % 3 != 0){
                queue.add(out);
            }

        }

        return queue.poll();
    }
}
